
var MongoClient = require('mongodb').MongoClient;

function dbClient(host, db) {
	if(host){
		this._host = host;
	}else{
		
	}
	
	if(db){
		this._db = db;
	}else{
		
	}

};

dbClient.prototype.insert = function insert(collection, info, options, callback) {
	if ('function' === typeof options) callback = options, options = {};
	if(options == null) options = {};
	if(!('function' === typeof callback)) callback = null;
	//console.log("insert " + this._host + this._db);
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
		//	console.log('db is connected!');
			this._collection = db.collection(collection);
			this._collection.insert(info, options, function(err2, result){			
					callback(err1, err2, result);
					db.close();
			});
		} else {
			console.log(err1);
			callback(err1);
		}
	});
};

dbClient.prototype.findOne = function(collection, query,  options, callback){
	if ('function' === typeof options) callback = options, options = {};
	if(options == null) options = {};
	if(!('function' === typeof callback)) callback = null;
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected!');
			this._collection = db.collection(collection);

			this._collection.findOne(query, options, function(err2, result) {
				callback(err1, err2, result);
				db.close();
			});
		} else {
			console.log(err1);
			callback(err1);
		}
	});
};

dbClient.prototype.find = function(col, query, options, callback) {
	if ('function' === typeof options) callback = options, options = {};
	if(options == null) options = {};
	if(!('function' === typeof callback)) callback = null;
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected');
			this._collection = db.collection(col);
			this._collection.find(query, options).toArray(function(err2, doc) {
				if (!err2) {
					db.close();
					return callback(err1,err2,doc);
				} else {
					console.log(err2);
					db.close();
					return callback(err1,db);
				}

			});
		} else
			console.log(err1);
	});
};

dbClient.prototype.remove = function(col, selector, options, callback) {
	if ('function' === typeof options) callback = options, options = {};
	if(options == null) options = {};
	if(!('function' === typeof callback)) callback = null;
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('db is connected');
			this._collection = db.collection(col);
			this._collection.remove(selector, opt,
					function(err2, numberOfRemoveDocs) {
						if (!err2) {
							db.close();
							return callback(err1,err2,numberOfRemoveDocs);
						} else {
							console.log(err2);
							db.close();
							return callback(err1,err2,null);
						}
					});
		} else
			console.log(err1);
	});
};

dbClient.prototype.update = function(col, selector, document, options, callback) {
	if ('function' === typeof options) callback = options, options = {};
	if(options == null) options = {};
	if(!('function' === typeof callback)) callback = null;
	MongoClient.connect(this._host + this._db, function(err1, db) {
		if (!err1) {
			console.log('it is connected');
			this._collection = db.collection(col);
			this._collection.update(selector, documents, options, function(err2, doc) {
				if (err2)
					console.log('update success');
				else {
					console.log('nothing found');
				}
			});
		} else {
			console.log(err1);
			db.close();
			return callback(err1,err2, null);

		}
	});
}
module.exports = dbClient;